package hah;

public class WaiGuanShuLie {
    public String countAndSay(int n) {
            if (n==1){
                return 1+"";
            }
            String s=countAndSay(n-1);
            char[] chars = s.toCharArray();
            StringBuffer sb=new StringBuffer();
            int count = 0;
            char temp=' ';
            int i=0;
            while(i<chars.length){
                if (i==0){
                    count=1;
                    temp=chars[i];
                    if (chars.length==1){
                        sb.append(count);
                        sb.append(temp);
                    }
                }
                else if (chars[i]==chars[i-1]){
                    count++;
                    if (chars.length==i+1){
                        sb.append(count);
                        sb.append(temp);
                    }
                }else {
                    sb.append(count);
                    sb.append(temp);
                    count=1;
                    temp=chars[i];
                    if (chars.length==i+1){
                        sb.append(count);
                        sb.append(temp);
                    }
                }
            i++;
            }

            return sb.toString();
    }

    public static void main(String[] args) {
        String s = new WaiGuanShuLie().countAndSay(4);
        System.out.println(s);
    }
}
